﻿unit SmOrderMstService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  SmOrderMstModel,
  SmOrderMstBLL,
  SmOrderModel,
  SmOrderBLL,
  SmOrderDtlService,
  CmPackageAccountService,
  SmOrderChargeService,
  CmPackageAccountModel;
type


  TSmOrderMstService = class(TSmOrderMstBLL)
  private
  
  protected
  
  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    
    function Check(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  
    function BeforeExecute(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  
    function AfterTreatment(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}
  end;


  TSmOrderService = class(TSmOrderBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function GenerateBillId(out oNewId,oError:string):Boolean;
  {$ENDIF}

  end;
  
implementation

{$IFDEF SERVER}
function TSmOrderMstService.BeforeTreatment(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.BeforeTreatment(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.BeforeCheck(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.BeforeCheck(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.Check(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.Check(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.BeforeExecute(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.BeforeExecute(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.AfterTreatment(const iItem:TSmOrderMst; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderMstService.AfterTreatment(const iItems:TSmOrderMstList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

{$IFDEF SERVER}
function TSmOrderService.BeforeTreatment(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  NewId,Key:string;
  SortNo:Integer;
  OrderDtlService:TSmOrderDtlService;
  OrderChargeService:TSmOrderChargeService;
  PackageAccountService:TCmPackageAccountService;
  OrderAmount:Real;
  PackageAccountItem:TCmPackageAccount;
  ItemDB:TSmOrder;
begin
  if iItem<>nil then
  begin
    OrderDtlService:=TSmOrderDtlService.Create(fServConn,fDBConn,fDBID);
    OrderChargeService:=TSmOrderChargeService.Create(fServConn,fDBConn,fDBID);
    PackageAccountService:=TCmPackageAccountService.Create(fServConn,fDBConn,fDBID);

    ItemDB:=TSmOrder.Create;
    try
      case iPreMode of
      pmNone: ;
      pmAdd:
        begin
          if not GenerateBillId(NewId,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          iItem.OrderId:=NewId;

          SortNo:=1;
          OrderAmount:=0;
          for Key in iItem.OrderDetailList.SortKeys do
          begin
            iItem.OrderDetailList.Items[Key].OrderId:=iItem.OrderId;
            iItem.OrderDetailList.Items[Key].OrderSortNo:=SortNo;
            if OrderDtlService.BeforeTreatment(iItem.OrderDetailList.Items[Key],pmAdd,iParcel,oError) then
            begin
              OrderAmount:=OrderAmount+iItem.OrderDetailList.Items[Key].OrderDtlAmount;
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
            Inc(SortNo);
          end;

          SortNo:=1;
          for Key in iItem.OrderChargeList.SortKeys do
          begin
            iItem.OrderChargeList.Items[Key].OrderId:=iItem.OrderId;
            iItem.OrderChargeList.Items[Key].OrderChargeSortNo:=SortNo;
            if OrderChargeService.BeforeTreatment(iItem.OrderChargeList.Items[Key],pmAdd,iParcel,oError) then
            begin
              OrderAmount:=OrderAmount+iItem.OrderChargeList.Items[Key].OrderChargeAmount;
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
            Inc(SortNo);
          end;
          iItem.OrderAmount:=OrderAmount;

          iItem.PackageAccountList.Clear;
          for Key in iItem.OrderDetailList.SortKeys do
          begin
            if iItem.OrderDetailList.Items[Key].OrderDtlQty<>0 then
            begin
              PackageAccountItem:=TCmPackageAccount.Create;
              PackageAccountItem.CustomerId:=iItem.CustomerId;                                     //关联的客户信息
              PackageAccountItem.PackageTypeId:=iItem.OrderDetailList.Items[Key].PackageTypeId;     //封装类型
              PackageAccountItem.PackageAccountType:=1;                                            //台账类型:1:销售发瓶
              PackageAccountItem.PackageAccountDirection:=1;                                       //台账方向:1:入账
              PackageAccountItem.PackageAccountQty:=iItem.OrderDetailList.Items[Key].OrderDtlQty;  //数量
              PackageAccountItem.OrgId:=iItem.OrderId;                                              //组织号
              PackageAccountItem.PackageAccountRemark:=Format(string('销售订单:%s 明细项:%s 发生的欠瓶记录'),    //备注
                                                                    [iItem.OrderId,iItem.OrderDetailList.Items[Key].OrderPk]);
              PackageAccountItem.PackageAccountRefType:=1;                       //关联单据类型:1:销售订单
              PackageAccountItem.PackageAccountRefId:=iItem.OrderId;            //关联单据号
              PackageAccountItem.PackageAccountCrtDate:=Now;          //发生时间
              iItem.PackageAccountList.Add(PackageAccountItem);
              if PackageAccountService.BeforeTreatment(PackageAccountItem,pmAdd,iParcel,oError) then
              begin

              end else begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
            if iItem.OrderDetailList.Items[Key].OrderDtlBackQty<>0 then
            begin
              PackageAccountItem:=TCmPackageAccount.Create;
              PackageAccountItem.CustomerId:=iItem.CustomerId;                                     //关联的客户信息
              PackageAccountItem.PackageTypeId:=iItem.OrderDetailList.Items[Key].PackageTypeId;     //封装类型
              PackageAccountItem.PackageAccountType:=2;                                            //台账类型:1:销售还空瓶
              PackageAccountItem.PackageAccountDirection:=-1;                                       //台账方向:-1:出账
              PackageAccountItem.PackageAccountQty:=iItem.OrderDetailList.Items[Key].OrderDtlBackQty;  //数量
              PackageAccountItem.OrgId:=iItem.OrderId;                                             //组织号
              PackageAccountItem.PackageAccountRemark:=Format(string('销售订单:%s 明细项:%s 发生的还瓶记录'),    //备注
                                                                    [iItem.OrderId,iItem.OrderDetailList.Items[Key].OrderPk]);
              PackageAccountItem.PackageAccountRefType:=1;                       //关联单据类型:1:销售订单
              PackageAccountItem.PackageAccountRefId:=iItem.OrderId;             //关联单据号
              PackageAccountItem.PackageAccountCrtDate:=Now;                     //发生时间
              iItem.PackageAccountList.Add(PackageAccountItem);
              if PackageAccountService.BeforeTreatment(PackageAccountItem,pmAdd,iParcel,oError) then
              begin

              end else begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
          end;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          iItem.ModifyDate:=Now;
          SortNo:=1;
          OrderAmount:=0;
          for Key in iItem.OrderDetailList.SortKeys do
          begin
            iItem.OrderDetailList.Items[Key].OrderId:=iItem.OrderId;
            iItem.OrderDetailList.Items[Key].OrderSortNo:=SortNo;
            Inc(SortNo);
            if ItemDB.OrderDetailList.IsExist(Key) then
            begin
              //更新
              iParcel.PutStringGoods(AnsiString(ItemDB.OrderDetailList.Items[Key].ClassName),ItemDB.OrderDetailList.Items[Key].SaveToStr);
              if not OrderDtlService.BeforeTreatment(iItem.OrderDetailList.Items[Key],pmEdit,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end else begin
              //新增
              if not OrderDtlService.BeforeTreatment(iItem.OrderDetailList.Items[Key],pmAdd,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
            OrderAmount:=OrderAmount+iItem.OrderDetailList.Items[Key].OrderDtlAmount;
          end;
          for Key in ItemDB.OrderDetailList.SortKeys do
          begin
            if not iItem.OrderDetailList.IsExist(Key) then
            begin
              //删除
              iParcel.PutStringGoods(AnsiString(ItemDB.OrderDetailList.Items[Key].ClassName),ItemDB.OrderDetailList.Items[Key].SaveToStr);
              if not OrderDtlService.BeforeTreatment(ItemDB.OrderDetailList.Items[Key],pmDelete,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
          end;


          SortNo:=1;
          for Key in iItem.OrderChargeList.SortKeys do
          begin
            iItem.OrderChargeList.Items[Key].OrderId:=iItem.OrderId;
            iItem.OrderChargeList.Items[Key].OrderChargeSortNo:=SortNo;
            Inc(SortNo);

            if ItemDB.OrderChargeList.IsExist(Key) then
            begin
              //更新
              iParcel.PutStringGoods(AnsiString(ItemDB.OrderChargeList.Items[Key].ClassName),ItemDB.OrderChargeList.Items[Key].SaveToStr);
              if not OrderChargeService.BeforeTreatment(iItem.OrderChargeList.Items[Key],pmEdit,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end else begin
              //新增
              if not OrderChargeService.BeforeTreatment(iItem.OrderChargeList.Items[Key],pmAdd,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
            OrderAmount:=OrderAmount+iItem.OrderChargeList.Items[Key].OrderChargeAmount;
          end;
          for Key in ItemDB.OrderChargeList.SortKeys do
          begin
            if not iItem.OrderChargeList.IsExist(Key) then
            begin
              //删除
              iParcel.PutStringGoods(AnsiString(ItemDB.OrderChargeList.Items[Key].ClassName),ItemDB.OrderChargeList.Items[Key].SaveToStr);
              if not OrderChargeService.BeforeTreatment(ItemDB.OrderChargeList.Items[Key],pmDelete,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
          end;
          iItem.OrderAmount:=OrderAmount;

          iItem.PackageAccountList.Clear;
          for Key in iItem.OrderDetailList.SortKeys do
          begin
            if iItem.OrderDetailList.Items[Key].OrderDtlQty<>0 then
            begin
              PackageAccountItem:=TCmPackageAccount.Create;
              PackageAccountItem.CustomerId:=iItem.CustomerId;                                     //关联的客户信息
              PackageAccountItem.PackageTypeId:=iItem.OrderDetailList.Items[Key].PackageTypeId;     //封装类型
              PackageAccountItem.PackageAccountType:=1;                                            //台账类型:1:销售发瓶
              PackageAccountItem.PackageAccountDirection:=1;                                       //台账方向:1:入账
              PackageAccountItem.PackageAccountQty:=iItem.OrderDetailList.Items[Key].OrderDtlQty;  //数量
              PackageAccountItem.OrgId:=iItem.OrderId;                                              //组织号
              PackageAccountItem.PackageAccountRemark:=Format(string('销售订单:%s 明细项:%s 发生的欠瓶记录'),    //备注
                                                                    [iItem.OrderId,iItem.OrderDetailList.Items[Key].OrderPk]);
              PackageAccountItem.PackageAccountRefType:=1;                       //关联单据类型:1:销售订单
              PackageAccountItem.PackageAccountRefId:=iItem.OrderId;            //关联单据号
              PackageAccountItem.PackageAccountCrtDate:=Now;          //发生时间
              iItem.PackageAccountList.Add(PackageAccountItem);
              if PackageAccountService.BeforeTreatment(PackageAccountItem,pmAdd,iParcel,oError) then
              begin

              end else begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;

            if iItem.OrderDetailList.Items[Key].OrderDtlBackQty<>0 then
            begin
              PackageAccountItem:=TCmPackageAccount.Create;
              PackageAccountItem.CustomerId:=iItem.CustomerId;                                     //关联的客户信息
              PackageAccountItem.PackageTypeId:=iItem.OrderDetailList.Items[Key].PackageTypeId;     //封装类型
              PackageAccountItem.PackageAccountType:=2;                                            //台账类型:1:销售还空瓶
              PackageAccountItem.PackageAccountDirection:=-1;                                       //台账方向:-1:出账
              PackageAccountItem.PackageAccountQty:=iItem.OrderDetailList.Items[Key].OrderDtlBackQty;  //数量
              PackageAccountItem.OrgId:=iItem.OrderId;                                             //组织号
              PackageAccountItem.PackageAccountRemark:=Format(string('销售订单:%s 明细项:%s 发生的还瓶记录'),    //备注
                                                                    [iItem.OrderId,iItem.OrderDetailList.Items[Key].OrderPk]);
              PackageAccountItem.PackageAccountRefType:=1;                       //关联单据类型:1:销售订单
              PackageAccountItem.PackageAccountRefId:=iItem.OrderId;             //关联单据号
              PackageAccountItem.PackageAccountCrtDate:=Now;                     //发生时间
              iItem.PackageAccountList.Add(PackageAccountItem);
              if PackageAccountService.BeforeTreatment(PackageAccountItem,pmAdd,iParcel,oError) then
              begin

              end else begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
          end;
          for Key in ItemDB.PackageAccountList.SortKeys do
          begin
            if not iItem.PackageAccountList.IsExist(Key) then
            begin
              //删除
              iParcel.PutStringGoods(AnsiString(ItemDB.PackageAccountList.Items[Key].ClassName),ItemDB.PackageAccountList.Items[Key].SaveToStr);
              if not PackageAccountService.BeforeTreatment(ItemDB.PackageAccountList.Items[Key],pmDelete,iParcel,oError) then
              begin
                oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
                Result:=False;
                Exit;
              end;
            end;
          end;
        end;
      pmDelete:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          for Key in ItemDB.OrderDetailList.SortKeys do
          begin
            iParcel.PutStringGoods(AnsiString(ItemDB.OrderDetailList.Items[Key].ClassName),ItemDB.OrderDetailList.Items[Key].SaveToStr);
            if not OrderDtlService.BeforeTreatment(ItemDB.OrderDetailList.Items[Key],pmDelete,iParcel,oError) then
            begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end;

          for Key in ItemDB.OrderChargeList.SortKeys do
          begin
            iParcel.PutStringGoods(AnsiString(ItemDB.OrderChargeList.Items[Key].ClassName),ItemDB.OrderChargeList.Items[Key].SaveToStr);
            if not OrderChargeService.BeforeTreatment(ItemDB.OrderChargeList.Items[Key],pmDelete,iParcel,oError) then
            begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end;

          for Key in ItemDB.PackageAccountList.SortKeys do
          begin
            iParcel.PutStringGoods(AnsiString(ItemDB.PackageAccountList.Items[Key].ClassName),ItemDB.PackageAccountList.Items[Key].SaveToStr);
            if not PackageAccountService.BeforeTreatment(ItemDB.PackageAccountList.Items[Key],pmDelete,iParcel,oError) then
            begin
              oError:=Format(string('调用%s.BeforeTreatment方法失败 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end;
        end;
      pmSendApprove: ;
      pmApprove: ;
      pmUnApprove: ;
      pmCancel: ;
      pmUnCancel: ;
      pmPrint: ;
      pmUnPrint: ;
      pmAccount: ;
      pmUnAccount: ;
      pmIgnore: ;
      pmUnIgnore: ;
      pmUse: ;
      pmUnUse: ;
      end;
    finally
      FreeAndNil(OrderDtlService);
      FreeAndNil(OrderChargeService);
      FreeAndNil(PackageAccountService);
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment单例方法失败 错误内容:传入的字符串或对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderService.BeforeTreatment(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.BeforeCheck(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.BeforeCheck(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.Check(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.Check(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.BeforeExecute(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  if iItem<>nil then
  begin
    case iPreMode of
    pmNone: ;
    pmAdd:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmEdit:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmDelete:
      begin
      end;
    pmSendApprove: ;
    pmApprove: ;
    pmUnApprove: ;
    pmCancel: ;
    pmUnCancel: ;
    pmPrint: ;
    pmUnPrint: ;
    pmAccount: ;
    pmUnAccount: ;
    pmIgnore: ;
    pmUnIgnore: ;
    pmUse: ;
    pmUnUse: ;
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute方法失败 错误内容:传入的字符串或对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderService.BeforeExecute(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.AfterTreatment(const iItem:TSmOrder; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.AfterTreatment(const iItems:TSmOrderList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSmOrderService.GenerateBillId(out oNewId,oError:string):Boolean;
var
  SQL,MaxOrderId:string;
  MaxOrderNo:Integer;
begin
  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s LIKE ''%s'';'),
              [TSmOrderMstModel.cOrderIdKey,TSmOrderMstModel.cOrderIdKey,
              TSmOrderMstModel.cTblName,TSmOrderMstModel.cOrderIdKey,
              'ORDER'+FormatDateTime(string('yyyyMMdd'),Now)+'%']);
  if TSK0001Comm.ReadSimpleData(fDBConn,EmptyStr,SQL,MaxOrderId,oError) then
  begin
    if SameStr(MaxOrderId,EmptyStr) then
    begin
      oNewId:='ORDER'+FormatDateTime(string('yyyyMMdd'),Now)+Format(string('%.4d'),[1]);
    end else if TryStrToInt(RightStr(MaxOrderId,4),MaxOrderNo) then
    begin
      oNewId:='ORDER'+FormatDateTime(string('yyyyMMdd'),Now)+Format(string('%.4d'),[MaxOrderNo+1]);
    end else begin
      oError:=Format(string('调用%s.GenerateBillId方法失败,错误内容:生成新订单号失败!'),[Self.ClassName]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.GenerateBillId方法失败,错误内容:获取最大订单号失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{$ENDIF}
  
end.
